/**
 * Created by TomSean on 2017/5/12.
 */
var ModuleSortManager = function ($parent) {
    this.sysUtils = new kendoUtils();
    this.$parent = $parent;
    this.init();
    return this;
}
ModuleSortManager.prototype = {
    init : function () {
        var $this = this;
        JsonpAjax(baseApiPath + '/module/sort/list', undefined, 'json', function(res) {
            var list = res;
            for (var i = 0; i < list.length; i++) {
                var item = list[i];
                var panel = '';
                try {
                    panel = '<div class="dd_card" data-value="'+(i+1)+'" data-module="'+item.module.id+'">';
                } catch (e) {
                    continue;
                }
                panel += '  <div class="title"><span>'+(i+1) + ' - ' + item.module.resourceName+'</span></div>';
                // panel += '      <span class="more"><ul class="select"><li><a onclick="moduleSortSetImport(\'' + item.id + '\', this)">置顶到</a></li><li><a onclick="moduleSortDeleteSort(\''+item.id+'\', this)">删除记录</a></li></ul></span>';
                panel += '</div>';
                if (!isEmpty(item.manualSort) && item.manualSort == 1) {
                    $this.$parent.find('#person .dd_center_card').append(panel);
                } else {
                    $this.$parent.find('#autoSort .dd_center_card').append(panel);
                }
            }
            var len = $('.dd_frame .dd_card').length;
            var cardWidth = 250, cardHeight = 350;
            var temp = len;
            $('.dd_frame .dd_card').each(function(inx, itm){
                var scale = 1-(0.2*(inx/2));
                if (scale == 1) {
                    scale = 1.1;
                }
                $(itm).css({'left':(inx * (cardWidth )) + 'px', 'transform':'scale('+scale+', '+scale+')', 'z-index':temp});
                if (inx != 0) {
                    $(itm).css({'background':'#eeeeee', 'box-shadow':'none'});
                } else {
                    $(itm).css({'background':'#ffffff', 'box-shadow':'0 0 85px #666'});
                }
                temp--;
            });
            $('.dd_right').bind('click', function(e){
                var cur = parseInt($(this).parents('.dd_frame').attr('data-cur'));
                if ( cur < len-1 ) {
                    cur++;
                    $(this).parents('.dd_frame').attr('data-cur', cur);
                    $(this).parents('.dd_frame').find('.dd_card').each(function(inx, itm){
                        var t = inx - cur;
                        var i = len - Math.abs(t);
                        var scale = 1-(0.2*(Math.abs(t)/2));
                        if (scale == 1) {
                            scale = 1.1;
                        }
                        $(itm).css({'left':(t * (cardWidth )) + 'px', 'transform':'scale('+scale+', '+scale+')', 'z-index':i});
                        if (inx != cur) {
                            $(itm).css({'background':'#eeeeee', 'box-shadow':'none'});
                        } else {
                            $(itm).css({'background':'#ffffff', 'box-shadow':'0 0 85px #666'});
                        }
                    });
                }
            });
            $('.dd_left').bind('click', function(e){
                var cur = parseInt($(this).parents('.dd_frame').attr('data-cur'));
                if ( cur > 0 ) {
                    cur--;
                    $(this).parents('.dd_frame').attr('data-cur', cur);
                    $(this).parents('.dd_frame').find('.dd_card').each(function(inx, itm){
                        var t = inx - cur;
                        var i = len - Math.abs(t);
                        var scale = 1-(0.2*(Math.abs(t)/2));
                        if (scale == 1){
                            scale = 1.1;
                        }
                        $(itm).css({'left':(t * (cardWidth )) + 'px', 'transform':'scale('+scale+', '+scale+')', 'z-index':i});
                        if (inx != cur) {
                            $(itm).css({'background':'#eeeeee', 'box-shadow':'none'});
                        } else {
                            $(itm).css({'background':'#ffffff', 'box-shadow':'0 0 85px #666'});
                        }
                    });
                }
            });

            $('.dd_card').bind('click', function(e){

            });
            $('.dd_center_card').bind('click', function(){
                var cur = parseInt($(this).parents('.dd_frame').attr('data-cur'));
                var id = $(this).find('.dd_card:eq('+cur+')').attr('data-module');
                $('a[data-id="'+id+'"]').click();
            });
            if ($this.$parent.find('#person .dd_center_card .dd_card').length < 1) {
                $this.$parent.find('#person .dd_center_card').append('<p style="font-size:24px; line-height: 250px;">暂无数据</p>');
            } else {

            }
            if ($this.$parent.find('#autoSort .dd_center_card .dd_card').length < 1) {
                $this.$parent.find('#autoSort .dd_center_card').append('<p style="font-size: 24px; line-height: 250px;">暂无数据</p>');
            } else {

            }
        }, true);
    },
};
function moduleSortDeleteSort (id, a) {
    $(a).parents('.sort-panel').hide();
    JsonpAjax(baseApiPath + '/module/sort/delete/' + id, '', 'json', function(res) {

    }, true);
};
function moduleSortSetImport (id, a) {
    JsonpAjax(baseApiPath + '/module/sort/import/' + id, '', 'json', function(res) {

    }, true);
}